package com.chenjt.minio.file;

import io.minio.*;
import io.minio.errors.MinioException;

import java.io.ByteArrayInputStream;

/**
 * 注释：
 *
 * @author chenjt
 * @date 2021/11/6 10:02
 */
public class Test {



    public static void main(String[] args)
            throws Exception {


        putObject();

//        removeObject();


    }

    private static void removeObject() throws Exception{

        try {
            // Create a minioClient with the MinIO server playground, its access key and secret key.
            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint("http://192.168.204.147:9000")
                            .credentials("minioadmin", "minioadmin")
                            .build();

            // Make 'asiatrip' bucket if not exist.
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket("chuanbo-picture").build());
            if (!found) {
                // Make a new bucket called 'asiatrip'.
                minioClient.makeBucket(MakeBucketArgs.builder().bucket("chuanbo-picture").build());
            } else {
                System.out.println("Bucket 'asiatrip' already exists.");
            }

            // Upload '/home/user/Photos/asiaphotos.zip' as object name 'asiaphotos-2015.zip' to bucket
            // 'asiatrip'.

            minioClient.removeObject(
                    RemoveObjectArgs.builder()
                            .bucket("chuanbo-picture")
                            .object("微信图片_20210510173524.jpg")
                            .build());
            //


            System.out.println(
                    "'/home/user/Photos/asiaphotos.zip' is successfully uploaded as "
                            + "object 'asiaphotos-2015.zip' to bucket 'asiatrip'.");
        } catch (MinioException e) {
            System.out.println("Error occurred: " + e);
            System.out.println("HTTP trace: " + e.httpTrace());
        }


    }


    private static void putObject() throws Exception{

        try {
            // Create a minioClient with the MinIO server playground, its access key and secret key.
            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint("http://192.168.204.147:9000")
                            .credentials("minioadmin", "minioadmin")
                            .build();

            // Make 'asiatrip' bucket if not exist.
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket("chuanbo-picture").build());
            if (!found) {
                // Make a new bucket called 'asiatrip'.
                minioClient.makeBucket(MakeBucketArgs.builder().bucket("chuanbo-picture").build());
            } else {
                System.out.println("Bucket 'asiatrip' already exists.");
            }

            // Upload '/home/user/Photos/asiaphotos.zip' as object name 'asiaphotos-2015.zip' to bucket
            // 'asiatrip'.
//            minioClient.uploadObject(
//                    UploadObjectArgs.builder()
//                            .bucket("chuanbo-picture")
//                            .object("chuanbo-picture/20210515/微信图片_20210510173524.jpg")
//                            .filename("C:\\Users\\chenjt\\Pictures\\微信图片_20210510173524.jpg")
//                            .build());
            //


            minioClient.putObject(
                    PutObjectArgs.builder().bucket("chuanbo-picture").object("test.jpg").stream(
                            new ByteArrayInputStream(new byte[] {}), 0, -1)
                            .build());

            System.out.println(
                    "'/home/user/Photos/asiaphotos.zip' is successfully uploaded as "
                            + "object 'asiaphotos-2015.zip' to bucket 'asiatrip'.");
        } catch (MinioException e) {
            System.out.println("Error occurred: " + e);
            System.out.println("HTTP trace: " + e.httpTrace());
        }

    }

}
